<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="200px">
      <el-form-item label="名称：" prop="name">
        <el-input type="text" style="width: 400px" v-model="form.name" maxlength="30" show-word-limit/>
      </el-form-item>
      <el-form-item label="封面：" prop="cover">
        <uploadimg :showFilelist="false" @setImg="setImg" :dialogImageUrl="form.cover"/>
        <span style="color: #999999">建议尺寸300x750高宽，大小2M以内</span>
      </el-form-item>
      <!--      <el-form-item label="链接：" prop="url">-->
      <!--        <el-input v-model="form.url" style="width: 400px" min="0" type="text"/>-->
      <!--      </el-form-item>-->
      <el-form-item label="内容：" prop="content">
        <Tinymce v-model="form.content" :height="300" id="Tinymce"></Tinymce>
      </el-form-item>
      <el-form-item label="上架：">
        <el-checkbox v-model="form.enable">是</el-checkbox>
      </el-form-item>
      <el-form-item label="排序：" prop="queue">
        <el-input v-model="form.queue" style="width: 150px" min="0" type="number"/>
        <div style="color:rgba(1,1,1,0.5)">值越大越靠前</div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import uploadimg from '@/components/UploadImg/index.vue'
  import Tinymce from '@/components/Tinymce'
  import { getToken } from '../../utils/auth'
  import { edit, detail } from '@/api/promotion'

  export default {
    data() {
      return {
        loading: false,
        form: {
          name: '',
          cover: '',
          content: '',
          enable: false,
          queue: '',
          token: getToken()
        },
        rules: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '请上传活动封面', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '请输入活动链接', trigger: 'blur' }
          ],
          queue: [
            { required: true, message: '请输入活动序号', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      uploadimg,
      Tinymce
    },
    methods: {
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.loading = true
            edit(this.form).then(res => {
              this.$message.success('添加成功')
              this.$router.go(-1)
            }).finally(
              this.loading = false
            )
          }
        })
      },
      setImg(url) {
        this.form.cover = url
      },
      onCancel() {
        this.$message({
          message: 'cancel!',
          type: 'warning'
        })
      }
    },
    created() {
      if(this.$route.query.id){
        detail({targetId:this.$route.query.id,token:getToken()}).then(res=>{
          this.form=res.data
          this.form.promotionId=res.data.id
          this.form.token=getToken()
        })
      }
    }
  }
</script>

<style scoped>
  .line {
    text-align: center;
  }
</style>

